<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签名称" prop="tagName">
        <el-input
          v-model="queryParams.tagName"
          placeholder="请输入标签名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-finished"
          size="mini"
          :disabled="!ids.length"
          @click="handleBatchAudit"
          v-hasPermi="['tags:changed:approve']"
        >批量审核</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tagChangeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="companyName" />
      <el-table-column label="标签名" align="center" prop="tagName" />
      <el-table-column label="标签类型" align="center" prop="tagType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tag_type" :value="scope.row.tagType"/>
        </template>
      </el-table-column>
      <el-table-column label="当前值" align="center" prop="currentValue" />
      <el-table-column label="修改待审值" align="center" prop="pendingValue" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 查看标签变更详情对话框 -->
    <el-dialog title="标签变更详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="企业名称">{{ form.companyName }}</el-descriptions-item>
        <el-descriptions-item label="标签名称">{{ form.tagName }}</el-descriptions-item>
        <el-descriptions-item label="申请类型">
          <dict-tag :options="dict.type.tag_change_type" :value="form.changeType"/>
        </el-descriptions-item>
        <el-descriptions-item label="审核状态">
          <dict-tag :options="dict.type.audit_status" :value="form.status"/>
        </el-descriptions-item>
        <el-descriptions-item label="申请原因" :span="2">{{ form.changeReason }}</el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ parseTime(form.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
        <el-descriptions-item label="审核时间" v-if="form.auditTime">{{ parseTime(form.auditTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
        <el-descriptions-item label="审核意见" :span="2" v-if="form.auditRemark">{{ form.auditRemark }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog :title="auditTitle" :visible.sync="auditOpen" width="500px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="80px">
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="auditForm.status">
            <el-radio label="1">审核通过</el-radio>
            <el-radio label="2">审核不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="auditRemark">
          <el-input v-model="auditForm.auditRemark" type="textarea" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAudit">确 定</el-button>
        <el-button @click="cancelAudit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChangedCompanyTags, pageTagChange, auditTagChange } from "@/api/biz/api";

export default {
  name: "TagChanged",
  dicts: ['tag_change_type', 'audit_status', 'tag_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签变更表格数据
      tagChangeList: [],
      // 查看对话框
      viewOpen: false,
      // 审核对话框
      auditOpen: false,
      // 审核对话框标题
      auditTitle: "",
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        companyName: null,
        tagName: null
      },
      // 表单参数
      form: {},
      // 审核表单参数
      auditForm: {
        changeId: null,
        status: "1",
        auditRemark: ""
      },
      // 审核表单校验
      auditRules: {
        status: [
          { required: true, message: "请选择审核结果", trigger: "change" }
        ],
        auditRemark: [
          { required: true, message: "请输入审核意见", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 批量审核
    handleBatchAudit() {
      if (!this.ids.length) {
        this.$modal.msgWarning('请先选择需要审核的记录');
        return;
      }
      this.resetAudit();
      // 批量审核默认通过，可在弹窗切换结果
      this.auditTitle = '批量审核';
      this.auditOpen = true;
    },
    /** 查询标签变更列表 */
    getList() {
      this.loading = true;
      listChangedCompanyTags(this.queryParams).then(response => {
        if(response.code == 200) {
          this.tagChangeList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }else{
          this.$modal.msgError(response.msg);
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancelAudit() {
      this.auditOpen = false;
      this.resetAudit();
    },
    // 审核表单重置
    resetAudit() {
      this.auditForm = {
        changeId: null,
        status: "1",
        auditRemark: ""
      };
      this.resetForm("auditForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.form = row;
      this.viewOpen = true;
    },
    /** 审核通过按钮操作 */
    handleApprove(row) {
      this.resetAudit();
      const changeId = row.id || this.ids[0];
      this.auditForm.changeId = changeId;
      this.auditForm.status = "1";
      this.auditTitle = "审核通过";
      this.auditOpen = true;
    },
    /** 审核不通过按钮操作 */
    handleReject(row) {
      this.resetAudit();
      const changeId = row.id || this.ids[0];
      this.auditForm.changeId = changeId;
      this.auditForm.status = "2";
      this.auditTitle = "审核不通过";
      this.auditOpen = true;
    },
    /** 提交审核 */
    submitAudit() {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          auditTagChange(this.auditForm).then(response => {
            this.$modal.msgSuccess("审核成功");
            this.auditOpen = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>
